package com.example.test;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintingWithReentrant {
    private static final Semaphore semaphoreNumber=new Semaphore(1);
    private static final Semaphore semaphoreLetter=new Semaphore(0);
    public static void main(String[] args) {
//        Lock lock=new ReentrantLock();
//        Condition numCondition=lock.newCondition();
//        Condition letterCondition=lock.newCondition();
        Thread thread1=new Thread(()->{
            for (int i=1;i<=3;i++){
                try {
                    semaphoreNumber.acquire();
                    System.out.print(i);
                    semaphoreLetter.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        Thread thread2=new Thread(()->{
            for (char letter='A';letter<='C';letter++){
                try {
                    semaphoreLetter.acquire();
                    System.out.print(letter);
                    semaphoreNumber.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}
